package com.yxqz.xfz.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 鱼香茄子
 * @data 2021/10/26
 * RabbitmqConfig 配置类
 */
@Configuration
public class RabbitmqConfig {

//    定义消息队列
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String EXCHANGE_TOPICS_INFORM="exchange_springboot_topics_inform";
    public static final String ROUTING_KEY_EMAL="inform.#.email.#"; // inform.email;inform.email.ccc;
    public static final String ROUTING_KEY_SMS="inform.#.sms.#";



    /**声明交换机
     *  Exchange: org.springframework.amqp.core.Exchange;
     *  durable(true) :持久化连接
     * @return
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange  EXCHANGE_TOPICS_INFORM (){
//         topic类型的交换机
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }


    /**声明队列 email
     *    Queue :org.springframework.amqp.core.Queue;
     */
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL (){
        return  new Queue(QUEUE_INFORM_EMAIL);
    }



    /**声明队列 短信的队列
     */
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS (){
        return  new Queue(QUEUE_INFORM_SMS);
    }


    /**
     *  email队列绑定交换机
     *  Qualifier 在spirng中通过Bean的名称注入
     *  noargs 不指定参数
     * @return
     */
    @Bean
    public Binding  BindingROUTING_KEY_EMAL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_EMAL).noargs();

    }



    /**
     *  sms队列绑定交换机
     *  Qualifier 在spirng中通过Bean的名称注入
     *  noargs 不指定参数
     * @return
     */
    @Bean
    public Binding  BindingROUTING_KEY_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_SMS).noargs();

    }

}
